package com.android.xunxian.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.xunxian.adapter.BoxMxAdpter;
import com.android.xunxian.entry.BoxMxListBean;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class BoxMxListActivity extends Activity {

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.listView)
    ListView listView;
    private String id;
    ProgressDialog dialog;

    private BoxMxAdpter boxMxAdpter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getIntent().getStringExtra("id");
        setContentView(R.layout.box_mxlist_activity);
        ButterKnife.inject(this);
        titleText.setText("表箱明细");
        if (TextUtils.isEmpty(id)) {
            finish();
            return;
        }
        getDate(id);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //获取设备列表
    private void getDate(String devid) {
        dialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.BOX_MX_LIST);
        params.addBodyParameter("LKind", "CBJ");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("BOXID", devid);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<BoxMxListBean>>() {
                            }.getType();
                            List<BoxMxListBean> list = gson.fromJson(object.getString("rows").toString(), lt);
//
                            if (list != null && list.size() > 0) {
//
                                System.out.println(list);
                                boxMxAdpter = new BoxMxAdpter(BoxMxListActivity.this,list);
                                listView.setAdapter(boxMxAdpter);

                            } else {
                                Toast.makeText(x.app(), "暂无数据！", Toast.LENGTH_LONG).show();
                            }

                        } else {
                            Toast.makeText(x.app(), "暂无数据！", Toast.LENGTH_LONG).show();
                        }
//                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                dialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取数据失败！", Toast.LENGTH_LONG).show();
                dialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                dialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
